﻿using Halcon二次开发.Controller.通信;
using Halcon二次开发.Tools;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Shapes;

namespace Halcon二次开发.View.Socket通信
{
    public partial class SocketForm : Form
    {
        //Josn文件保存的路径
        public string jsonPath = @"..\..\Data\Socket数据\Socket.json";
        //Json文件保存发送消息记录路径
        public string jsonSendPath = @"..\..\Data\Socket数据\Send.json";
        //Json文件保存接收消息记录路径
        public string jsonReceivePath = @"..\..\Data\Socket数据\Receive.json";
        //发送消息记录
        public List<string> listSendRecord = new List<string>();
        //接收消息记录
        public List<string> listReceiveRecord = new List<string>();
        public SocketForm()
        {
            InitializeComponent();
            SocketController.GetSocketController().SetSocketForm(this);
            CheckForIllegalCrossThreadCalls = false;
            //初始化我们的dataGideView控件
            InitializeComponentDataGridView();
        }
        //单例模式，通信界面对象
        private static SocketForm socketForm;
        public static SocketForm GetSocketForm()
        {
            if (socketForm==null)
            {
                socketForm=new SocketForm();
                if (File.Exists(socketForm.jsonPath))
                {
                    Dictionary<string, string> dis = HelpJsons.Read(socketForm.jsonPath);
                    if (dis!=null)
                    {
                        new StoreFormHelp().MemoryMap(socketForm,socketForm.jsonPath);
                        //socketForm.Connect();
                    }
                }
            }
            return socketForm;
        }
        //连接成功bool标志位
        public bool connectPaper = false;
        /// <summary>
        /// 连接按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Connect();
        }
        /// <summary>
        /// 连接方法
        /// </summary>
        private void Connect()
        {
            connectPaper = SocketController.GetSocketController().Connect(ipTextBox.Text.Trim(), portTextBox.Text.Trim());
            if (connectPaper)
            {
                toolStripStatusLabel2.Text = "已连接";
                toolStripStatusLabel2.ForeColor = Color.Green;
                toolStripStatusLabel4.Text = ipTextBox.Text.Trim();
                toolStripStatusLabel6.Text = portTextBox.Text.Trim();
            }
        }
        private void SocketForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (SocketController.GetSocketController().Sends(sendTextBox.Text.Trim()))
                {
                    listSendRecord.Add(HelpGetTiem.Getyyymmddmmss() + "-$&^&$-" + sendTextBox.Text.Trim());
                    dataGridView1.Rows.Add(HelpGetTiem.Getyyymmddmmss(),sendTextBox.Text.Trim());
                    //清除文本
                    sendTextBox.Clear();
                }
            }
            catch (Exception ex)
            {
                Reset();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Reset();
        }
        /// <summary>
        /// 复位信息
        /// </summary>
        public void Reset()
        {
            //将信息复位
            toolStripStatusLabel2.Text = "未连接";
            toolStripStatusLabel2.ForeColor = Color.Red;
            toolStripStatusLabel4.Text = "无信息";
            toolStripStatusLabel6.Text = "无信息";
        }
        public void InitializeComponentDataGridView()
        {
            List<string> l1=HelpJsons.Read<string>(jsonSendPath);
            List<string> l2=HelpJsons.Read<string>(jsonReceivePath);
            if (l1!=null)
            {
                listSendRecord = l1;
                foreach (string item in listSendRecord)
                {
                    string[] strs=item.Split("-$&^&$-");
                    dataGridView1.Rows.Add(strs[0], strs[1]);
                }
            }
            if (l2!=null)
            {
                listReceiveRecord = l2;
                foreach (string item in listReceiveRecord)
                {
                    string[] strs = item.Split("-$&^&$-");
                    dataGridView2.Rows.Add(strs[0], strs[1]);
                }
            }
        }
        //清除接收记录
        private void button4_Click(object sender, EventArgs e)
        {
            string s=MessageBox.Show("是否清除接收消息记录", "消息", MessageBoxButtons.YesNo).ToString();
            if (s.Equals("Yes"))
            {
                dataGridView2.Rows.Clear();
                listReceiveRecord.Clear();  
            }
        }
        //记录发送记录
        private void button5_Click(object sender, EventArgs e)
        {
            string s = MessageBox.Show("是否清除发送消息记录", "消息", MessageBoxButtons.YesNo).ToString();
            if (s.Equals("Yes"))
            {
                dataGridView1.Rows.Clear();
                listSendRecord.Clear();
            }
        }

        private void SocketForm_Load(object sender, EventArgs e)
        {

        }
    }
}
